//
//  YAYIUserCommentController.m
//  yayi
//
//  Created by Veer on 16/7/4.
//  Copyright © 2016年 Veer. All rights reserved.
//

#import "YAYIUserCommentController.h"

#import "YAYIUserCommentCell.h"
#import "YAYIUserCommentModel.h"

@interface YAYIUserCommentController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak) UILabel *noDataFlagLbl;

@property (nonatomic,strong)NSMutableArray *commentArray;

@end

@implementation YAYIUserCommentController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"用户评价";
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.commentArray = [NSMutableArray array];

    [self getUserInfo];
}


- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"暂时没有用户评价";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-64);
    }];
}


#pragma mark - 获取数据
- (void)getUserInfo
{
    // 显示菊花
    [SVProgressHUD show];
    
    [YAYINetWorking getWithURL:me_personalinfo param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回：%@", dict);
            NSString *doctorID = [NSString stringWithFormat:@"%@", dict[@"data"][@"id"]];
            [self getCommentByUserID:doctorID];
            
        }
        [SVProgressHUD dismiss];
    }];
}

-(void)getCommentByUserID:(NSString *)userID
{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
//    param[@"doctorid"] = self.doctorid;
    param[@"doctorid"] = userID;
    
    [YAYINetWorking  getWithURL:doctorComment_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (dict != nil) {
            
            YAYILog(@"评价数据：%@", dict);
            
            NSArray *da = dict[@"data"];
            for (NSDictionary *obj in da) {
                YAYIUserCommentModel *model = [YAYIUserCommentModel new];
                [model setValuesForKeysWithDictionary:obj];
                [self.commentArray addObject:model];
            }
            
            if (0 == self.commentArray.count) {
                [self showNoDataFlag:YES];
                
            }else{
                [self showNoDataFlag:NO];
            }
            
            [self.tableView reloadData];
        }
        
    }];


}


#pragma mark - 代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.commentArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"identifer";
    YAYIUserCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[YAYIUserCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    cell.model = self.commentArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIUserCommentModel *model = self.commentArray[indexPath.row];
    CGFloat height = [self heightForRow:model.words font:[UIFont systemFontOfSize:font14] width:SCREEN_width - 30];
    return 44 + height;
}


-(CGFloat)heightForRow:(NSString *)str font:(UIFont *)font width:(CGFloat)width
{
    CGSize size = [str sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    return size.height;
}


@end
